<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>红绿灯切换</title>
    <style>
        .main{
            display: flex;
            border: 1px solid #ccc;
            flex-direction: column;
            align-items: center;
        }
        .main .light{
            display: flex;
            flex-direction: row;
        }
        .main .light div{
            height: 60px;
            width: 60px;
            background-color: lightgrey;
            margin-left: 10px;
            margin-bottom: 10px;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="light"> 
            <div id="red"></div>
            <div id="green"></div>
            <div id="yellow"></div>
        </div>
        <div class="buttons">
            <input type="button" value="红灯停" id="red-button" onclick="switchColor('red')"> 
            <input type="button" value="绿灯行" id="green-button" onclick="switchColor('green')">
            <input type="button" value="黄灯亮了等一等" id="yellow-button" onclick="switchColor('yellow')">
        </div>
    </div>
</body>
<script>
    function switchColor(color){
        console.log(color)
        // 按钮颜色初始化
        document.getElementById('red-button').style.backgroundColor = "lightgrey"
        document.getElementById('green-button').style.backgroundColor = "lightgrey"
        document.getElementById('yellow-button').style.backgroundColor = "lightgrey"
        // 灯的颜色初始化
        document.getElementById('red').style.backgroundColor = "lightgrey"
        document.getElementById('green').style.backgroundColor = "lightgrey"
        document.getElementById('yellow').style.backgroundColor = "lightgrey"
        // 条件判断语句
        if(color == "red"){
            document.getElementById('red-button').style.backgroundColor = color
            document.getElementById('red').style.backgroundColor = color
        }else if(color == "yellow"){
            document.getElementById('yellow-button').style.backgroundColor = color
            document.getElementById('yellow').style.backgroundColor = color
        }else{
            document.getElementById('green-button').style.backgroundColor = color
            document.getElementById('green').style.backgroundColor = color
        }
    }
</script>
</html>